package com.example.flex.controller.generator;

import cn.hutool.core.util.ObjectUtil;
import com.example.flex.domain.generator.DbConnectionEntity;
import com.example.flex.service.generator.DbConnectionService;
import com.example.flex.utils.common.PageEntity;
import com.example.flex.utils.response.BaseResponse;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 数据库连接信息表控制层
 * @Author: 不是菜鸡爱编程
 * @Date:
 * @Description：
 */
@RestController
@Api(tags = "数据库连接信息表接口")
@RequestMapping("dbConnection")
public class DbConnectionController{

    @Autowired
    private DbConnectionService dbConnectionService;

    /**
     * 保存
     *
     * @param dbConnectionEntity 数据库连接信息表
     * @return BaseResponse
     */
    @PostMapping("/save")
    @ApiOperation("保存数据库连接信息表")
    public BaseResponse<?> save(@RequestBody @ApiParam("数据库连接信息表") DbConnectionEntity dbConnectionEntity){
        boolean saveFlag = dbConnectionService.save(dbConnectionEntity);
        if(saveFlag){
            return BaseResponse.success("保存成功");
        }else{
            return BaseResponse.error("保存失败");
        }
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return BaseResponse
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据主键查询数据库连接信息表")
    public BaseResponse<?> info(@PathVariable @ApiParam("数据库连接信息表主键") Integer id){
        return BaseResponse.success(dbConnectionService.getById(id),"查询成功");
    }

    /**
     * 列表查询
     *
     * @return BaseResponse
     */
    @PostMapping("/list")
    @ApiOperation("列表查询数据库连接信息表")
    public BaseResponse<?> list(@RequestBody @ApiParam("数据库连接信息表分页参数") PageEntity<DbConnectionEntity> dbConnectionEntityPage){
        DbConnectionEntity dbConnectionEntity = dbConnectionEntityPage.getData();
        QueryWrapper queryWrapper =getQueryWrapper(dbConnectionEntity);
        return BaseResponse.success(dbConnectionService.page(new Page<>(dbConnectionEntityPage.getPageNum(),dbConnectionEntityPage.getPageSize()),queryWrapper),"查询成功");
    }

    /**
     * 删除
     *
     * @param ids
     * @return BaseResponse
     */
    @DeleteMapping("/delete/{ids}")
    @ApiOperation("删除数据库连接信息表")
    public BaseResponse<?> delete(@PathVariable @ApiParam("数据库连接信息表主键(列表)") Integer[] ids){
        return BaseResponse.success(dbConnectionService.removeByIds(Arrays.asList(ids)),"删除成功");
    }


    /**
     * 修改
     *
     * @param dbConnectionEntity 数据库连接信息表
     * @return BaseResponse
     */
    @PutMapping("/edit")
    @ApiOperation("修改数据库连接信息表")
    public BaseResponse<?> edit(@RequestBody @ApiParam("数据库连接信息表") DbConnectionEntity dbConnectionEntity){
        return BaseResponse.success(dbConnectionService.updateById(dbConnectionEntity),"修改成功");
    }

    /**
     * 获取queryWrapper
     * 当检索条件不为空时，进行匹配
     *
     * @param dbConnectionEntity 数据库连接信息表
     * @return {@link QueryWrapper}
     */
    private QueryWrapper getQueryWrapper(DbConnectionEntity dbConnectionEntity){
        return QueryWrapper.create()
                    .eq(DbConnectionEntity::getId,dbConnectionEntity.getId(), ObjectUtil.isNotEmpty(dbConnectionEntity.getId()))
                    .eq(DbConnectionEntity::getDbName,dbConnectionEntity.getDbName(),ObjectUtil.isNotEmpty(dbConnectionEntity.getDbName()))
                    .eq(DbConnectionEntity::getDbType,dbConnectionEntity.getDbType(),ObjectUtil.isNotEmpty(dbConnectionEntity.getDbType()))
                    .eq(DbConnectionEntity::getIpAddress,dbConnectionEntity.getIpAddress(),ObjectUtil.isNotEmpty(dbConnectionEntity.getIpAddress()))
                    .eq(DbConnectionEntity::getDbPort,dbConnectionEntity.getDbPort(),ObjectUtil.isNotEmpty(dbConnectionEntity.getDbPort()))
                    .eq(DbConnectionEntity::getUsername,dbConnectionEntity.getUsername(),ObjectUtil.isNotEmpty(dbConnectionEntity.getUsername()))
                    .eq(DbConnectionEntity::getPassword,dbConnectionEntity.getPassword(),ObjectUtil.isNotEmpty(dbConnectionEntity.getPassword()))
                    .eq(DbConnectionEntity::getStatus,dbConnectionEntity.getStatus(),ObjectUtil.isNotEmpty(dbConnectionEntity.getStatus()))
                    .eq(DbConnectionEntity::getRemark,dbConnectionEntity.getRemark(),ObjectUtil.isNotEmpty(dbConnectionEntity.getRemark()))
                    .eq(DbConnectionEntity::getCreateTime,dbConnectionEntity.getCreateTime(),ObjectUtil.isNotEmpty(dbConnectionEntity.getCreateTime()))
                    .eq(DbConnectionEntity::getUpdateTime,dbConnectionEntity.getUpdateTime(),ObjectUtil.isNotEmpty(dbConnectionEntity.getUpdateTime()));
    }
}